package com.atguigu.write;

import com.atguigu.bean.Movie;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Index;

import java.io.IOException;
import java.util.HashMap;

public class Es01_SingleWrite {
    public static void main(String[] args) throws IOException {
        //1.创建客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        //设置连接属性
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //2.获取客户端连接
        JestClient jestClient = jestClientFactory.getObject();

        //3.写入单条数据
        //以JavaBean的方式写入数据
        /*Movie movie = new Movie("103", "金瓶梅");
        Index index = new Index.Builder(movie)
                .index("movie")
                .type("_doc")
                .id("1003")
                .build();*/


        //以map的方式写入数据
        HashMap<String, String> map = new HashMap<>();
        map.put("id", "102");
        map.put("name", "西游记");
        Index index = new Index.Builder(map)
                .index("movie")
                .type("_doc")
                .id("1002")
                .build();

        //以json字符串方式写入数据
       /* Index index = new Index.Builder("{\n" +
                "  \"id\":\"101\",\n" +
                "  \"name\":\"鲁滨逊漂流记\"\n" +
                "}")
                .index("movie")
                .type("_doc")
                .id("1001")
                .build();*/
        jestClient.execute(index);


        //关闭连接
        jestClient.shutdownClient();
    }
}
